<?php
namespace EasySwoole\EasySwoole;


use App\Utility\MyQueue;
use App\Utility\QueueProcess;
use EasySwoole\Component\Timer;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\Queue\Driver\Redis;
use EasySwoole\Queue\Job;
use EasySwoole\Redis\Config\RedisConfig;
use EasySwoole\RedisPool\RedisPool;
use EasySwoole\Utility\Time;
use EasySwoole\EasySwoole\Config;

class EasySwooleEvent implements Event
{

    public static function initialize()
    {
        // TODO: Implement initialize() method.
        date_default_timezone_set('Asia/Shanghai');
    }

    public static function mainServerCreate(EventRegister $register)
    {
        // TODO: Implement mainServerCreate() method.
        //redis pool使用请看redis 章节文档
        //获取配置
        $confInstance = Config::getInstance();
        $redisHost = $confInstance->getConf('queue.redis.host');
        $queuekey = $confInstance->getConf('queue.queuekey');

        $config = new RedisConfig([
            'host' => $redisHost
        ]);
        $redis = new RedisPool($config);
        $driver = new Redis($redis, $queuekey);
        MyQueue::getInstance($driver);
        //注册一个消费进程
        ServerManager::getInstance()->addProcess(new QueueProcess());
        // //模拟生产者，可以在任意位置投递
        // $register->add($register::onWorkerStart,function ($ser,$id){
        //     if($id == 0){
        //         Timer::getInstance()->loop(3000,function (){
        //            $job = new Job();
        //            $job->setJobData(['time'=>\time()]);
        //            MyQueue::getInstance()->producer()->push($job);
        //         });
        //     }
        // });
    }

    public static function onRequest(Request $request, Response $response): bool
    {
        // TODO: Implement onRequest() method.
        return true;
    }

    public static function afterRequest(Request $request, Response $response): void
    {
        // TODO: Implement afterAction() method.
    }
}